package org.keyyond.ms.business.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.keyyond.ms.business.model.MsBusinessDocument;
import org.keyyond.ms.business.model.MsBusinessMessageLog;
import org.keyyond.ms.business.service.MsbusinessEsbFormatConfigService;
import org.keyyond.ms.business.util.CommonBeanutils;
import org.keyyond.ms.business.util.Constant;
import org.keyyond.ms.core.util.ConstantResult;
import org.keyyond.ms.core.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("format")
@RestController
public class MsBusinessFormatDataController extends CommonBeanutils{
	
	@Autowired
	private MsbusinessEsbFormatConfigService esbFormatConfigService;
	
	/**
   	 * 将对象格式化成xml或者json格式数据
   	 * 
   	 * @author songjianping
   	 * @since 2018-11-19
   	 * @category </br>将对象格式化成xml或者json格式数据</br>
   	 *           
   	 * @param request
   	 *            | HttpServletRequest | 必填  {data:xxx,type:1/2,pkmangedatainfo:xxx}
   	 * @param response
   	 *            | HttpServletResponse | 必填  
   	 * @return <b>String</br>
   	 * @exception 
   	 */
    @RequestMapping(value = "/formatData", method = RequestMethod.POST)
    public String formatData(HttpServletRequest request, HttpServletResponse response) {
	response.setHeader("Access-Control-Allow-Origin", "*");
	String result = new String();
	JSONObject resultData = new JSONObject();
	String traceId = request.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
	MsBusinessMessageLog messageLog = new MsBusinessMessageLog();
	messageLog.setTraceid(traceId);
	messageLog.setLogtype(Constant.CONST_MESSAGE_LOG_EXCEPTION);
	String param = request.getParameter(Constant.CONST_ZBGS_PARAM);
	List<MsBusinessDocument> docs = new ArrayList<MsBusinessDocument>();
	try {
	    Map<String, Object> paramMap = JsonUtil.readJson2Map(param);
		StringBuffer msg = new StringBuffer();
		if(paramMap.get("data")==null||StringUtils.isEmpty(String.valueOf(paramMap.get("data")))){
			msg.append("原始数据不能为空");
		}
		if(paramMap.get("type")==null||StringUtils.isEmpty(String.valueOf(paramMap.get("type")))){
			msg.append("格式化类型不能为空");
		}
		if(paramMap.get("pkmangedatainfo")==null||StringUtils.isEmpty(String.valueOf(paramMap.get("pkmangedatainfo")))){
			msg.append("格式化类型配置信息主键不能为空");
		}
		if(StringUtils.isEmpty(msg.toString())){
			Map data = (Map)paramMap.get("data");
			String type = String.valueOf(paramMap.get("type"));
			String pkmangedatainfo = String.valueOf(paramMap.get("pkmangedatainfo"));
			String formatStr = null;
			if(type.equals(Constant.CONST_BUSINESS_XML)){
				formatStr=esbFormatConfigService.createXml(pkmangedatainfo,data);
			}else if(type.equals(Constant.CONST_BUSINESS_JSON)){
				formatStr=esbFormatConfigService.createJson(pkmangedatainfo,data);
			}
			System.out.println(formatStr);
			resultData.put("record", formatStr);
			result = returnResult(true, resultData);
		}else{
		    resultData.put(ConstantResult.RESULT_ERRMSG, msg.toString());
		    resultData.put(ConstantResult.RESULT_ERRCODE, ConstantResult.RESULT_ERRCODE_ACTION);
		    result = returnResult(false, resultData);
		}
		messageLog.setLogtype(Constant.CONST_MEESAGE_LOG_SERVICE);
	} catch (Exception e) {
	    e.printStackTrace();
	    resultData.put(ConstantResult.RESULT_ERRMSG, ConstantResult.RESULT_ERRMSG_EXCEPTION);
	    resultData.put(ConstantResult.RESULT_ERRCODE, ConstantResult.RESULT_ERRCODE_EXCEPTION);
	    result = returnResult(false, resultData);
	}
	messageLog.setReturnmessage(result);
	addMessageLog(messageLog);
	return result;
    } 

}
